
export class EventDispatch<V> {
    private map: Map<string | number | symbol, ((...arg: any) => any)[]> = new Map();
    addEventListener<K extends keyof V>(type: K, listener: (e: V[K]) => any): void {
        let array = this.map.get(type);
        if (array) {
            let index = array.indexOf(listener);
            if (index === -1) {
                array.push(listener);
            }
        } else {
            this.map.set(type, [listener]);
        }
    }
    removeEventListener<K extends keyof V>(type: K, listener: (e: V[K]) => any): void {
        let array = this.map.get(type);
        if (array) {
            let index = array.indexOf(listener);
            if (index !== -1) {
                array.splice(index, 1);
            }
        }
    }
    removeAllListener<K extends keyof V>(type: K) {
        this.map.delete(type);
    }

    dispatch<K extends keyof V>(type: K, e: V[K]) {
        let array = this.map.get(type);
        if (array) {
            array.forEach(f => f?.(e));
        }
    }
}
